"""
Frame for text input.
"""

import tkinter as tk
from ui.DataProvidingFrame import DataProvidingFrame
from ui.ByteGenToBitGen import byteGeneratorToBit

class CrcTextInputFrame(DataProvidingFrame):
    """This frame contains the interaction needed for the user to specify
    the item for wich he wants to calculate the CRC.

    Later, the frame should be created in a CrcInputFrame wich will
    contain this method of input, and this class will contain a way to
    switch from this pannel to the file choice pannel."""

    def get_help_message(self):
        return """All characters are allowed."""

    def getDataGenerator(self):
        """Override from DataProvidingFrame.
        Return a generator (or None) for the data."""
        data = self.inputBox.get(1.0,tk.END)
        def gen():
            for i in data[:-1]:
                yield ord(i)
        return byteGeneratorToBit(gen())()

    def getDataSize(self):
        """Override from DataProvidingFrame.
        Return the size of the data."""
        data = self.inputBox.get(1.0,tk.END)
        return (len(data)-1)*8

    def create_input_instructions(self):
        """Create the message widget."""
        self.description = tk.Message(self)
        self.description["width"] = "180" # pixels
        self.description["text"] = "Please enter the string to encode:"
        self.description["anchor"] = "w"
        self.description.pack({"side":"top","padx":5,"pady":5,"fill":"x"})
    def create_input_box(self):
        """Create the text widget."""
        self.inputBox = tk.Text(self)
        self.inputBox["width"] = "35" # columns
        self.inputBox["height"] = "3" # lines
        self.inputBox.pack({"side":"bottom","padx":5,"pady":5,"fill":"both","expand":1})
    def create_widgets(self):
        """Create the frame starting layout."""
        self.create_input_instructions()
        self.create_input_box()
    def __init__(self,master=None,size=(100,100)):
        super().__init__(master,size)
        self.create_widgets()
        super().create_widgets()
